/**
 * 
 */
package edu.arizona.bio5.onto.decomposition.persistent.xml;

import java.io.Reader;
import java.io.StringReader;

import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Resource;
import org.xmldb.api.base.XMLDBException;

import edu.arizona.bio5.onto.decomposition.persistent.ADPersistenceException;
import edu.arizona.bio5.onto.decomposition.persistent.AbstractAtomLoader;
import edu.arizona.bio5.onto.decomposition.persistent.PersistentAtom;

/**
 * @author Pavel Klinov
 *
 */
public class XMLDBAtomLoader extends AbstractAtomLoader {

	private Collection mAtomCollection;
	
	protected XMLDBAtomLoader(Collection atomColl) {
		mAtomCollection = atomColl;
	}
	
	@Override
	protected Reader getOntologySourceReader(PersistentAtom atom) throws ADPersistenceException {
		try {
			Resource atomResource = mAtomCollection.getResource(XMLADConstants.ATOM_DOC_PREFIX + atom.getID().toString() + ".owl");
			
			return new StringReader(atomResource.getContent().toString());
		} catch (XMLDBException e) {
			throw new ADPersistenceException(e);
		}
	}

	@Override
	public void close() {
		if (mAtomCollection != null) {
			try {
				mAtomCollection.close();
			} catch (XMLDBException e) {}
		}
	}
}